using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using DailyReport.Core.Bussines.IManagers;
using DailyReport.Core.Data.Dao;
using DailyReport.Core.Data.Dao.DaoFactory;
using DailyReport.Core.Data.Model;
using log4net;

namespace DailyReport.Core.Bussines
{
    public class TeamManager:ITeamManager<Team>
    {
        private ILog log = LogManager.GetLogger("DailyReportManager");
        
        public void createTeam(int parentTeamId, string name, int managerId)
        {
            Team t=new Team();
            t.Name = name;
            ITeamDao teamDao = (ITeamDao)DaoFactory.i.getDaoInstance("TeamDao");
            
            if (parentTeamId > 0)
            {
                teamDao = (ITeamDao)DaoFactory.i.getDaoInstance("TeamDao");
                t.ParentTeam = teamDao.GetById(parentTeamId, false);
            }
            IUserDao userDao = (IUserDao) DaoFactory.i.getDaoInstance("UserDao");
            t.Manager=userDao.GetById(managerId, false);

            teamDao.Save(t);
        }

        public void updateTeam(int teamId, int parentTeamId, string name, int managerId)
        {
            ITeamDao teamDao = (ITeamDao)DaoFactory.i.getDaoInstance("TeamDao");
            Team t = teamDao.GetById(teamId, false);
            t.Name = name;
            
            if (parentTeamId > 0)
            {
                t.ParentTeam = teamDao.GetById(parentTeamId, false);
            }
            IUserDao userDao = (IUserDao)DaoFactory.i.getDaoInstance("UserDao");
            t.Manager = userDao.GetById(managerId, false);

            teamDao.SaveOrUpdate(t);
        }

        public void deleteTeam(int teamId)
        {
            ITeamDao teamDao = (ITeamDao)DaoFactory.i.getDaoInstance("TeamDao");
            Team t = teamDao.GetById(teamId, true);
            teamDao.Delete(t);
        }

        public Team loadTeam(int teamId)
        {
            ITeamDao teamDao = (ITeamDao)DaoFactory.i.getDaoInstance("TeamDao");
            Team t = teamDao.GetById(teamId, true);
            return (t);
        }

        public IList GetInstanceList(string sortExpression, int startRowIndex, int maxRows)
        {
            ITeamDao teamDao = (ITeamDao) DaoFactory.i.getDaoInstance("TeamDao");
            IList res = teamDao.getInstancesList(sortExpression, maxRows, startRowIndex);
            return(res);
        }

        public int CountInstances()
        {
            ITeamDao teamDao = (ITeamDao)DaoFactory.i.getDaoInstance("TeamDao");
            int res=teamDao.getInstancesCount();
            log.Warn("****#Teams contados:"+res);
            return (res);
        }
    }
}
